package com.wuba.rxjavademo.tzx.filter;

import com.wuba.rxjavademo.tzx.common.Base;

import java.util.concurrent.TimeUnit;

import rx.Observable;

/**
 * Created by tanzhenxing
 * Date: 2016/6/27.
 * Description:
 * debounce()仅在过了一段指定的时间还没发射数据时才发射一个数据，过滤掉由Observable发射的速率过快的数据
 */
public class RxDebounce extends Base {
    public static void main(String[] args) {
        RxDebounce debounce = new RxDebounce();
        debounce.run();
    }

    @Override
    public void run() {
        Observable.<Integer>create(subscriber -> {
                    for (int i = 2; i < 9; i++) {
                        subscriber.onNext(i);
                        try {
                            Thread.sleep(1000 * i);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    subscriber.onCompleted();
                })
                .debounce(3, TimeUnit.SECONDS)
                .subscribe(Base::print);
    }
}
